<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/30 0030
 * Time: 11:00
 */

namespace app\admin\controller;
use think\Db;

class Recycle extends Bases
{
    //咨询回收
    public function consultrecycle(){
        $data =recycle('consult');


        $this->assign('data',$data);
        return $this->fetch();
    }
    //咨询恢复
    public function consultrecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result=recovery('consult',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //咨询删除
    public function consultDel(){
        $id = intval(input('param.id'));
        if($id){
            $result = recycleDel('consule',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    public function delallconsult(){
        $ids = rtrim(input('post.ids'),',');
        if($ids){

            $where['id'] =['in' ,$ids];
            $result = Db::name('consult')->where($where)->delete();
        }else{
            return json(['code'=>0,'msg'=>'参数错误']);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }

    }
    //客照回收
    public function worksRecycle(){
        $data =recycle('works');
        $this->assign('data',$data);
        return $this->fetch();
    }
    //客照恢复
    public function worksRecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result = recovery('works',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //客照删除
    public function worksDel(){
        $id = intval(input('param.id'));
        if($id){
            //删除主图
            $imgs = Db::name('works')->where('id',$id)->field('main_img,works_photo')->find();
            if($imgs['main_img']){
                unlink('.'.str_replace('\\','/',$imgs['main_img']));
            }
            //删除多图
            if($imgs['works_photo']){
                    $imgurls = Db::name('works_pic')->where('pic_id','in',$imgs['works_photo'])->select();
                    foreach ($imgurls as $k=>$v){
                        if($v['works_pic'] !==' '){
                            unlink('.'.str_replace('\\','/',$v['works_pic']));
                        }
                    }
            }
            $result = recycleDel('works',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    public function delallworks(){
        $ids = rtrim(input('post.ids'),',');
        if($ids){
            $imgs = Db::name('works')->where('id','in',$ids)->field('main_img,works_photo')->select();
            foreach ($imgs as $v=>$v){
                if($v['main_img']){
                    unlink('.'.str_replace('\\','/',$imgs['main_img']));
                }
                if($v['works_photo']){
                    $imgurls = Db::name('works_pic')->where('pic_id','in',$v['works_photo'])->select();
                    foreach ($imgurls as $value){
                        if($value['works_pic']){
                            unlink('.'.str_replace('\\','/',$value['works_pic']));
                        }
                    }
                }

            }
            $where['id'] =['in' ,$ids];
            $result = Db::name('works')->where($where)->delete();
        }else{
            return json(['code'=>0,'msg'=>'参数错误']);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }

    }

    //获选作品回收
    public function hxzpRecycle(){
        $data =recycle('hxzp');
        $this->assign('data',$data);
        return $this->fetch();
    }
    //获选作品恢复
    public function hxzpRecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result = recovery('hxzp',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //获选作品删除
    public function hxzpDel(){
        $id = intval(input('param.id'));
        if($id){
            $imgs = Db::name('hxzp')->where('id',$id)->field('main_img,works_photo')->find();
            if($imgs['main_img'] !==' '){
                unlink('.'.str_replace('\\','/',$imgs['main_img']));
            }
            //删除多图
            if($imgs['works_photo']){
                $imgurls = Db::name('works_pic')->where('pic_id','in',$imgs['works_photo'])->select();
                foreach ($imgurls as $k=>$v){
                    if($v['works_pic'] !==' '){
                        unlink('.'.str_replace('\\','/',$v['works_pic']));

                    }
                }
            }
            $result = recycleDel('hxzp',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }


    public function delallhxzp(){
        $ids = rtrim(input('post.ids'),',');
        if($ids){
            $imgs = Db::name('hxzp')->where('id','in',$ids)->field('main_img,works_photo')->select();
            foreach ($imgs as $v=>$v){
                if($v['main_img']){
                    unlink('.'.str_replace('\\','/',$imgs['main_img']));
                }
                if($v['works_photo']){
                    $imgurls = Db::name('works_pic')->where('pic_id','in',$v['works_photo'])->select();
                    foreach ($imgurls as $value){
                        if($value['works_pic']){
                            unlink('.'.str_replace('\\','/',$value['works_pic']));
                        }
                    }
                }

            }
            $where['id'] =['in' ,$ids];
            $result = Db::name('hxzp')->where($where)->delete();
        }else{
            return json(['code'=>0,'msg'=>'参数错误']);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }

    }

    //作品回收
    public function zpRecycle(){
        $data = Recycle('zp');
        $this->assign('data',$data);
        return $this->fetch();
    }
    //作品恢复
    public function zpRecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result = recovery('zp',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //作品删除
    public function zpDel(){
        $id = intval(input('param.id'));
        if($id){
            $imgs = Db::name('zp')->where('id',$id)->field('main_img,works_photo')->find();
            if($imgs['main_img'] !==' '){
                unlink('.'.str_replace('\\','/',$imgs['main_img']));
            }
            //删除多图
            if($imgs['works_photo']){
                $imgurls = Db::name('works_pic')->where('pic_id','in',$imgs['works_photo'])->select();
                foreach ($imgurls as $k=>$v){
                    if($v['works_pic'] !==' '){
                        unlink('.'.str_replace('\\','/',$v['works_pic']));
                    }
                }
            }
            $result = recycleDel('zp',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    //作品批量删除
    public function delallzp(){
        $ids = rtrim(input('post.ids'),',');
        if($ids){
            $imgs = Db::name('zp')->where('id','in',$ids)->field('main_img,works_photo')->select();
            foreach ($imgs as $v=>$v){
                if($v['main_img']){
                    unlink('.'.str_replace('\\','/',$imgs['main_img']));
                }
                if($v['works_photo']){
                    $imgurls = Db::name('works_pic')->where('pic_id','in',$v['works_photo'])->select();
                    foreach ($imgurls as $value){
                        if($value['works_pic']){
                            unlink('.'.str_replace('\\','/',$value['works_pic']));
                        }
                    }
                }

            }
            $where['id'] =['in' ,$ids];
            $result = Db::name('zp')->where($where)->delete();
        }else{
            return json(['code'=>0,'msg'=>'参数错误']);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }

    }
    //拍摄基地回收
    public function cgRecycle(){
        $data = recycle('psjd');
        $this->assign('data',$data);
        return $this->fetch();
    }
    //拍摄基地恢复
    public function cgreRecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result = recovery('psjd',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //拍摄基地删除
    public function cgdel(){
        $id = intval(input('param.id'));
        if($id){
            $img = Db::name('psjd')->where('id',$id)->value('main_img');
            if($img !==' '){
                unlink('.'.str_replace('\\','/',$img));
            }
            $result = recycleDel('psjd',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }


    //拍摄基地批量删除
    public function delallcg(){
        $ids = rtrim(input('post.ids'),',');
        if($ids){
            $imgs = Db::name('psjd')->where('id','in',$ids)->field('main_img')->select();
            foreach ($imgs as $v=>$v){
                if($v['main_img']){
                    unlink('.'.str_replace('\\','/',$imgs['main_img']));
                }
            }
            $where['id'] =['in' ,$ids];
            $result = Db::name('psjd')->where($where)->delete();
        }else{
            return json(['code'=>0,'msg'=>'参数错误']);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }

    }
    //全球旅拍回收
    public function qqlpRecycle(){
        $data = recycle('qqlp');
        $this->assign('data',$data);
        return $this->fetch();
    }
    //全球旅拍恢复
    public function qqlpreRecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result = recovery('qqlp',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //全球旅拍删除
    public function qqlpdel(){
        $id = intval(input('param.id'));
        if($id){
            $img = Db::name('qqlp')->where('id',$id)->value('main_img');
            if($img !==' '){
                unlink('.'.str_replace('\\','/',$img));
            }
            $result = recycleDel('qqlp',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    //全球旅拍批量删除
    public function delallqqlp(){
        $ids = rtrim(input('post.ids'),',');
        if($ids){
            $imgs = Db::name('qqlp')->where('id','in',$ids)->field('main_img')->select();
            foreach ($imgs as $v=>$v){
                if($v['main_img']){
                    unlink('.'.str_replace('\\','/',$imgs['main_img']));
                }
            }
            $where['id'] =['in' ,$ids];
            $result = Db::name('qqlp')->where($where)->delete();
        }else{
            return json(['code'=>0,'msg'=>'参数错误']);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }

    }


    //艺术会所回收
    public function yshsRecycle(){
        $data = recycle('yshs');
        $this->assign('data',$data);
        return $this->fetch();
    }
    //艺术会所恢复
    public function yshsreRecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result = recovery('yshs',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //艺术会所删除
    public function yshsdel(){
        $id = intval(input('param.id'));
        if($id){
            $img = Db::name('yshs')->where('id',$id)->value('main_img');
            if($img !==' '){
                unlink('.'.str_replace('\\','/',$img));
            }
            $result = recycleDel('yshs',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    //艺术会所批量删除
    public function delallyshs()
    {
        $ids = rtrim(input('post.ids'), ',');
        if ($ids) {
            $imgs = Db::name('yshs')->where('id', 'in', $ids)->field('main_img')->select();
            foreach ($imgs as $v => $v) {
                if ($v['main_img']) {
                    unlink('.' . str_replace('\\', '/', $imgs['main_img']));
                }
            }
            $where['id'] = ['in', $ids];
            $result = Db::name('yshs')->where($where)->delete();
        } else {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        if ($result !== false) {
            return json(['code' => 1, 'msg' => '删除成功']);
        } else {
            return json(['code' => 0, 'msg' => '删除失败']);
        }
    }


        //活动回收
    public function activityRecycle(){
        $data = Db::name('activity')->where('is_del=1 and sign="pc"')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }
    //活动恢复
    public function activityRecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result = Db::name('activity')->where('id',$id)->update(['is_del'=>0]);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //活动删除
    public function activityDel(){
        $id = intval(input('param.id'));
        if($id){
            $img = Db::name('activity')->where('id',$id)->value('main_img');
            if($img !==' '){
                unlink('.'.str_replace('\\','/',$img));
            }
            $result = Db::name('activity')->where('id',$id)->delete();
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    //活动批量删除
    public function delallhd()
    {
        $ids = rtrim(input('post.ids'), ',');
        if ($ids) {
            $imgs = Db::name('activity')->where('id', 'in', $ids)->field('main_img')->select();
            foreach ($imgs as $v => $v) {
                if ($v['main_img']) {
                    unlink('.' . str_replace('\\', '/', $imgs['main_img']));
                }
            }
            $where['id'] = ['in', $ids];
            $result = Db::name('activity')->where($where)->delete();
        } else {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        if ($result !== false) {
            return json(['code' => 1, 'msg' => '删除成功']);
        } else {
            return json(['code' => 0, 'msg' => '删除失败']);
        }
    }

        //新闻资讯回收
    public function newsRecycle(){
        $data = recycle('news');
        $this->assign('data',$data);
        return $this->fetch();
    }
    //新闻资讯恢复
    public function newsreRecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result = recovery('news',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //新闻资讯删除
    public function newsdel(){
        $id = intval(input('param.id'));
        if($id){
            $img = Db::name('news')->where('id',$id)->value('main_img');
            echo $img;
            if($img !==' '){
                unlink('.'.str_replace('\\','/',$img));
            }
            $result = recycleDel('news',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }


    //新闻批量删除
    public function delallnews()
    {
        $ids = rtrim(input('post.ids'), ',');
        if ($ids) {
            $imgs = Db::name('news')->where('id', 'in', $ids)->field('main_img')->select();
            foreach ($imgs as $v => $v) {
                if ($v['main_img']) {
                    unlink('.' . str_replace('\\', '/', $imgs['main_img']));
                }
            }
            $where['id'] = ['in', $ids];
            $result = Db::name('news')->where($where)->delete();
        } else {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        if ($result !== false) {
            return json(['code' => 1, 'msg' => '删除成功']);
        } else {
            return json(['code' => 0, 'msg' => '删除失败']);
        }
    }
        //婚嫁知识回收
    public function articleRecycle(){
        $data = recycle('article');
        $this->assign('data',$data);
        return $this->fetch();
    }
    //婚嫁知识恢复
    public function articleRecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result = recovery('article',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //婚嫁知识删除
    public function articledel(){
        $id = intval(input('param.id'));
        if($id){
            $img = Db::name('article')->where('id',$id)->value('main_img');
            if($img !==' '){
                unlink('.'.str_replace('\\','/',$img));
            }
            $result = recycleDel('news',$id);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    //新闻批量删除
    public function delallwz(){
        $ids = rtrim(input('post.ids'),',');
        if($ids){
            $imgs = Db::name('article')->where('id','in',$ids)->field('main_img')->select();
            foreach ($imgs as $v=>$v){
                if($v['main_img']){
                    unlink('.'.str_replace('\\','/',$imgs['main_img']));
                }
            }
            $where['id'] =['in' ,$ids];
            $result = Db::name('article')->where($where)->delete();
        }else{
            return json(['code'=>0,'msg'=>'参数错误']);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }

    }


    //广告回收
    public function bannerRecycle(){
        $data = Db::name('banner')->where('is_del=1 and sign="pc"')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }
    //广告恢复
    public function bannerRecovery(){
        $id = intval(input('param.id'));
        if($id){
            $result = Db::name('banner')->where('id',$id)->update(['is_del'=>0]);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //广告删除
    public function bannerDel(){
        $id = intval(input('param.id'));
        if($id){
            $imgs = Db::name('banner')->where('id',$id)->field('banner_img，banner_img_mobile')->find();
            if($imgs['banner_img'] !==' '){

                unlink('.'.str_replace('\\','/',$imgs['banner_img']));
            }
            if($imgs['banner_img_mobile'] !==' '){

                unlink('.'.str_replace('\\','/',$img['banner_img_mobile']));
            }
            $result = Db::name('banner')->where('id',$id)->delete();
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

}